﻿using System.Collections.Generic;

namespace ExtremeSwank.Authentication.OpenID.Plugins.Discovery
{
    /// <summary>
    /// Represents a single discovered OpenID endpoint
    /// </summary>
    public struct DiscoveryEntry
    {
        string _OpenIDServer;
        string _DelegateID;
        int _Priority;
        ProtocolVersion _MaxVersion;

        /// <summary>
        /// Gets or sets the OpenID Server endpoint URL
        /// </summary>
        public string OpenIDServer
        {
            get { return _OpenIDServer; }
            set { _OpenIDServer = value; }
        }
        /// <summary>
        /// Gets or sets the discovered delegate ID (Local identifier)
        /// </summary>
        public string DelegateID
        {
            get { return _DelegateID; }
            set { _DelegateID = value; }
        }
        /// <summary>
        /// Gets or sets the priority of the entry
        /// </summary>
        public int Priority
        {
            get { return _Priority; }
            set { _Priority = value; }
        }
        /// <summary>
        /// Gets or sets the highest OpenID version supported by the endpoint
        /// </summary>
        public ProtocolVersion MaxVersion
        {
            get { return _MaxVersion; }
            set { _MaxVersion = value; }
        }

        /// <summary>
        /// Creates a new XRDSDiscoveryEntry
        /// </summary>
        /// <param name="server">OpenID Server URL</param>
        /// <param name="delegateid">Deletegate identifier (local ID)</param>
        /// <param name="priority">Priority of the entry</param>
        /// <param name="maxversion">Highest version supported by the endpoint</param>
        public DiscoveryEntry(string server, string delegateid, int priority, ProtocolVersion maxversion)
        {
            _OpenIDServer = server;
            _DelegateID = delegateid;
            _Priority = priority;
            _MaxVersion = maxversion;
        }
    }

    /// <summary>
    /// Compares two XRDSDiscoveryEntry objects.  
    /// </summary>
    /// <remarks>Provides ability to sort on Priority.</remarks>
    public class DiscoveryEntryComparer : IComparer<DiscoveryEntry>
    {
        #region IComparer<DiscoveryEntry> Members

        /// <summary>
        /// Compares two DiscoveryEntry objects.
        /// </summary>
        /// <param name="x">Object 1</param>
        /// <param name="y">Object 2</param>
        /// <returns>Result consistent with numerical comparisons</returns>
        public int Compare(DiscoveryEntry x, DiscoveryEntry y)
        {
            return x.Priority.CompareTo(y.Priority);
        }

        #endregion
    }
}
